package com.lazy.app.h5;

import android.app.Activity;
import android.util.Log;
import android.webkit.JavascriptInterface;

import androidx.annotation.NonNull;

import com.lazy.app.BuildConfig;
import com.zndroid.bridge.InvokeController;
import com.zndroid.bridge.api.BaseAPI;

public class LazyH5API extends BaseAPI {
    private final String url;
    private final InvokeController invokeController;

    public LazyH5API(Activity activity, InvokeController invokeController, @NonNull String url) {
        super(activity);
        this.url = url;
        this.invokeController = invokeController;
    }

    @Override
    protected String getTAG() {
        return getClass().getSimpleName();
    }

    @JavascriptInterface
    public void reload(Object o) {
        if (BuildConfig.DEBUG) {
            Log.i(getTAG(), o.toString());
        }

        invokeController.load(url);
    }

    @JavascriptInterface
    public void closeActivity(Object o) {
        if (BuildConfig.DEBUG) {
            Log.i(getTAG(), "closeActivity");
        }

        if (!activity.get().isDestroyed()) {
            activity.get().finish();
        }
    }
}
